package goActorsInstance

import (
	"goActorsCommon"
	"os"
)

type GoInstanceNaming struct {
	Gi	* GoInstance
}

func New_GoInstanceNaming( gi * GoInstance ) * GoInstanceNaming {
	gia := &GoInstanceNaming {
		Gi	: gi,
	}
	
	return gia
}

func (gia * GoInstanceNaming) Resolve ( name * goActorsCommon.NameWrap, result * goActorsCommon.PidWrap ) os.Error {
	localPid, exists := gia.Gi.Actors_Name[name.RealName]
	if ( exists ) {
		result.RealPid = goActorsCommon.New_GoActorPid( gia.Gi.Pid, localPid )
		return nil
	}
	// Resolve distributed
	return os.NewError( "Name not found." )
}

func (gia * GoInstanceNaming) Register ( args * goActorsCommon.RegisterArgsWrap, res * bool ) os.Error {
	actorPid, ok := args.RealPid.( * goActorsCommon.GoActorPid )
	if ( !ok ) {
		*res = false
		return os.NewError( "A GoInstance can only register Pid's of type GoActorPid." )
	}
	_, exists := gia.Gi.Actors_Name[args.RealName]
	if ( exists ) {
		*res = false
		return os.NewError( "Name allready in use." )
	}
	// Try register distributed
	gia.Gi.Actors_Name[args.RealName] = actorPid.GoActor
	*res = true
	return nil
}

